package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    /**
     *  游记分页查询
     * @param qo
     * @return
     */
    @RequestMapping("query")
    public Object query(TravelQuery qo){
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 富文本框图片上传
     */
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile,HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    /**
     * 添加/编辑游记
     */
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){

        // 获取当前登录用户id
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());

        travelService.saveOrUpdate(travel);

        return JsonResult.success(travel.getId());
    }

    /**
     * 查看游记明细
     */
    @GetMapping("detail")
    public Object getDetail(String id){

        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }

    /**
     * 使用简化方式获取当前登录用户信息对象
     * 贴了UserParam注解表示该参数对象由参数解析器注入
     */
    @GetMapping("info")
    public Object info(@UserParam UserInfo userInfo){
        System.out.println(userInfo);
        return JsonResult.success(userInfo);
    }


    /**
     * 查询所有评论
     */
    @GetMapping("comments")
    public Object comments(String travelId ){

        List<TravelComment> list = travelService.comments(travelId);
        return JsonResult.success(list);
    }

    /**
     * 发表回复
     */
    @RequireLogin
    @PostMapping("commentAdd")
    public Object commentAdd(TravelComment comment,@UserParam UserInfo userInfo){
        travelCommentService.addComment(comment,userInfo);
        return JsonResult.success();
    }



}
